<template>
  <a-modal
    title="复制页面节点"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="复制到页面" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <div :style="{ borderBottom: '1px solid #E9E9E9' }">
            <a-checkbox
              v-model:checked="checkAll"
              :indeterminate="indeterminate"
              @change="onCheckAllChange"
            >
              全选
            </a-checkbox>
          </div>
          <br />
          <a-checkbox-group v-model:value="checkedList" :options="pageList" />

        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'

export default {
  name:'copyNodeForm',
  data () {
    return {
      // 对话框标题
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: {
        span: 21
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      checkedList:[],
      pageList:[],
      indeterminate: false,
      checkAll: false,
    }
  },
  watch:{
    checkedList(val){
      this.indeterminate = !!val.length && val.length < this.pageList.length;
      this.checkAll = val.length === this.pageList.length;
    },
  },
  methods: {

    /**
     * 显示对话框
     */
    open (pageList) {
      // 显示窗口
      this.visible = true
      this.checkedList=[]
      // 当前记录
      this.pageList = pageList.map((row,idx)=>{
        this.checkedList.push(idx)
        return {label:row.name,value:idx}
      })
    },


    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      this.$emit('confirm',this.checkedList)
      this.visible=false;
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      values.id = this.record.id || 0

    },
    onCheckAllChange(e){
      Object.assign(this, {
        checkedList: e.target.checked ? this.pageList.map(row => row.value) : [],
        indeterminate: false,
      });
    }

  }
}
</script>
<style lang="less" scoped>
  /deep/ .ant-checkbox-group{
    overflow: scroll;
    max-height: 300px;
    label{font-size: 16px;}
  }
</style>